package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafeTrain;
import com.ruoyi.food.mapper.OpmFoodSafeTrainMapper;
import com.ruoyi.food.service.IOpmFoodSafeTrainService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 安全培训Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafeTrainServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafeTrainService {
    @Autowired
    private OpmFoodSafeTrainMapper opmFoodSafeTrainMapper;

    /**
     * 查询安全培训
     *
     * @param id 安全培训ID
     * @return 安全培训
     */
    @Override
    public OpmFoodSafeTrain selectOpmFoodSafeTrainById(Long id) {
        OpmFoodSafeTrain result = opmFoodSafeTrainMapper.selectOpmFoodSafeTrainById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询安全培训列表
     *
     * @param opmFoodSafeTrain 安全培训
     * @return 安全培训
     */
    @Override
    public List<OpmFoodSafeTrain> selectOpmFoodSafeTrainList(OpmFoodSafeTrain opmFoodSafeTrain) {
        return opmFoodSafeTrainMapper.selectOpmFoodSafeTrainList(opmFoodSafeTrain);
    }

    /**
     * 新增安全培训
     *
     * @param opmFoodSafeTrain 安全培训
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafeTrain(OpmFoodSafeTrain opmFoodSafeTrain) {
        opmFoodSafeTrain.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafeTrain.setCreateTime(DateUtils.getNowDate());
        opmFoodSafeTrain.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeTrain.getFileIdList(), opmFoodSafeTrain.getId().toString());
        return opmFoodSafeTrainMapper.insertOpmFoodSafeTrain(opmFoodSafeTrain);
    }

    /**
     * 修改安全培训
     *
     * @param opmFoodSafeTrain 安全培训
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafeTrain(OpmFoodSafeTrain opmFoodSafeTrain) {
        opmFoodSafeTrain.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafeTrain.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafeTrain.getFileIdList(), opmFoodSafeTrain.getId().toString());
        return opmFoodSafeTrainMapper.updateOpmFoodSafeTrain(opmFoodSafeTrain);
    }

    /**
     * 批量删除安全培训
     *
     * @param ids 需要删除的安全培训ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeTrainByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafeTrainMapper.deleteOpmFoodSafeTrainByIds(ids);
    }

    /**
     * 删除安全培训信息
     *
     * @param id 安全培训ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafeTrainById(Long id) {
        return opmFoodSafeTrainMapper.deleteOpmFoodSafeTrainById(id);
    }
}
